package com.daliu.controller;

import com.daliu.pojo.po.Role;
import com.daliu.pojo.vo.AddUser;
import com.daliu.pojo.vo.PageResult;
import com.daliu.pojo.vo.Result;
import com.daliu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;

    @GetMapping("/findByPage")
    public Result findByPage(@RequestParam("currentPage") int currentPage,
                             @RequestParam("pageSize") int pageSize){
       PageResult pageResult=  service.findByPage(currentPage,pageSize);
        return new Result(true,"查询成功",pageResult);
    }

    @GetMapping("/findAll")
    public List<Role> findAll(){
       return service.findALl();
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody AddUser addUser){
        service.addUser(addUser);

        return new Result(true,"添加成功");
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody AddUser addUser){
        int num = service.updateUser(addUser);
        return new Result(true,"修改成功",num);
    }

    @GetMapping("/deleteUserById")
    public Result deleteUserById(@RequestParam("id") int id){
        int num =  service.deleteById(id);
        System.out.println(num);
        return new Result(true,"删除成功",num);
    }
}
